﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerDinhMucThuDao : IDinhMucThuDao
    {
        public SqlServerDinhMucThuDao()
        {
            SQLUtility.TableName = "DinhMucThu";
        }

        //Trả về đối tượng báo ăn
        private DinhMucThu MakeDinhMucThu(DataRow row)
        {
            Guid _id = Guid.Parse(row["DinhMucID"].ToString());
            Guid _blockID = Guid.Parse(row["BlockID"].ToString());
            Guid _thuChiID = Guid.Parse(row["ThuChiID"].ToString());
            decimal _soluong = decimal.Parse(row["Soluong"].ToString());
            decimal _dinhmuc = decimal.Parse(row["Dinhmuc"].ToString());
            decimal _thanhtien = decimal.Parse(row["Thanhtien"].ToString());
            bool _selected = bool.Parse(row["Selected"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());
         return new DinhMucThu(_id, _blockID, _thuChiID, _soluong, _dinhmuc, _thanhtien, _selected, _schoolID);
        }
        private DbParameter[] Take(DinhMucThu DinhMucThu)
        {
            DbParameter[] parameters = new DbParameter[8];
            parameters[0] = new SqlParameter("@DinhMucID", DinhMucThu.Id);
            parameters[1] = new SqlParameter("@BlockID", DinhMucThu.BlockID);
            parameters[2] = new SqlParameter("@ThuChiID", DinhMucThu.ThuChiID);
            parameters[3] = new SqlParameter("@Soluong", DinhMucThu.Soluong);

            parameters[4] = new SqlParameter("@Dinhmuc", DinhMucThu.Dinhmuc);
            parameters[5] = new SqlParameter("@Thanhtien", DinhMucThu.Thanhtien);
            parameters[6] = new SqlParameter("@Selected", DinhMucThu.Selected);
            parameters[7] = new SqlParameter("@SchoolID", DinhMucThu.SchoolID);
            return parameters;
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<DinhMucThu> MakeDinhMucThus(DataTable dt)  
        {
            IList<DinhMucThu> list = new List<DinhMucThu>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeDinhMucThu(row));
            }
            return list;
        }
        public IList<Business_Objects.DinhMucThu> GetDinhMucThu()
        {
            SQLUtility.TableName = "DinhMucThu";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeDinhMucThus(dt); 
        }

        public Business_Objects.DinhMucThu GetDinhMucThuByID(string DinhMucThuID)
        {
            SQLUtility.TableName = "DinhMucThu";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@DinhMucID", DinhMucThuID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeDinhMucThu(row);
            }
        }

        public void InsertDinhMucThu(Business_Objects.DinhMucThu DinhMucThu)
        {
            SQLUtility.TableName = "DinhMucThu";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(DinhMucThu), true);
        }

        public int UpdateDinhMucThu(Business_Objects.DinhMucThu DinhMucThu)
        {
            SQLUtility.TableName = "DinhMucThu";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(DinhMucThu)); }
            catch { return -1; }
        }

        public int DeleteDinhMucThu(Business_Objects.DinhMucThu DinhMucThu)
        {
            SQLUtility.TableName = "DinhMucThu";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(DinhMucThu)); }
            catch { return -1; }
        }


        public IList<DinhMucThu> GetDinhMucThuBySchoolId(int schoolId)
        {
            SQLUtility.TableName = "DinhMucThu";
            var parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectDinhMucThusByAndSchoolID", parameters); //Cần sửa lại sto
            return MakeDinhMucThus(dt); 
        }


        public List<DinhMucThu> GetDinhMucThuBySchoolIdAndKhoi(int schoolId, string blockId)
        {
            SQLUtility.TableName = "DinhMucThu";
            var parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@BlockID", blockId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectDinhMucThusByAndBlockIDAndSchoolID", parameters); //Cần sửa lại sto
            return MakeDinhMucThus(dt).ToList(); 
        }
    }
}
